package com.bbx.taxi.client.Adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.bbx.api.gclient.GMsg;
import com.bbx.api.sdk.model.passanger.Return.Jmsg.Msgs;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.taxi.official.R;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.DB.Util.DBcolumns;
import com.bbx.taxi.client.Util.DateFormat;
import com.bbx.taxi.client.widget.MyViewPager;
import com.bbx.taxi.client.widget.CircleHead.CircleImageView;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 消息
 */
public class MessageAdapter extends BaseAdapter {
	private List<Map<String, String>> list;
	private Activity main;
	private boolean isItemClick = false;

	public MessageAdapter(Activity context, List<Map<String, String>> list) {
		this.list = list;
		main = context;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;

	}

	public boolean getItemClick() {
		return isItemClick;
	}

	public void setItemClick(boolean isItemClick) {
		this.isItemClick = isItemClick;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewholder;
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(main);
			convertView = (View) inflater.inflate(R.layout.item_message,
					null);
			viewholder = new ViewHolder();
			viewholder.head = (CircleImageView) convertView.findViewById(R.id.head);
			viewholder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			viewholder.tv_content = (TextView) convertView
					.findViewById(R.id.tv_content);
			viewholder.tv_time = (TextView) convertView
					.findViewById(R.id.tv_time);
			viewholder.iv_jump = (ImageView) convertView
					.findViewById(R.id.iv_jump);
			convertView.setTag(viewholder);
		} else
			viewholder = (ViewHolder) convertView.getTag();
		
		viewholder.iv_jump.setVisibility(View.VISIBLE);
		viewholder.tv_name.setText(main.getString(R.string.msg_remind));
		JSONObject jmsg = null;
		JSONObject jmsg_data=null;
		Msgs.Data msgs_data=null;
		try {
			jmsg = new JSONObject(list.get(position).get(DBcolumns.MESSAGE_JSON));
			jmsg_data = new JSONObject(jmsg.getString(GMsg.msg_data));
		 msgs_data = (Msgs.Data) new JsonBuild().getData(Msgs.Data.class,
				jmsg_data.toString());
		} 
		catch (Exception e) {
		}
		String readstatus=list.get(position).get(DBcolumns.MESSAGE_READ);
		if(Value.MSG_READ.equals(readstatus)){//已读
			viewholder.tv_name.setTextColor(main.getResources().getColor(R.color.notes));
			viewholder.tv_content.setTextColor(main.getResources().getColor(R.color.notes));
		}
		else{
			viewholder.tv_name.setTextColor(main.getResources().getColor(R.color.black));
			viewholder.tv_content.setTextColor(main.getResources().getColor(R.color.dark_notes));
		}
		int type=Integer.parseInt(list.get(position).get(DBcolumns.MESSAGE_TYPE));
		switch(type){
		case GMsg.OFFICAIL_ONLINE_PAIDAN:
			viewholder.tv_name.setText(main.getString(R.string.msg_remind));
			viewholder.head.setImageResource(R.drawable.icon_news);
			if(msgs_data!=null){
				String name=msgs_data.getDriver_name();
				viewholder.tv_content.setText(String.format(main.getString(R.string.order_paidan_msg),name,msgs_data.getCar_NO()));
			}
			break;
		case GMsg.OFFICAIL_ONLINE_CANCEL:
		case GMsg.ONLINE_KF_CANCEL:
			viewholder.tv_name.setText(main.getString(R.string.msg_remind));
			viewholder.head.setImageResource(R.drawable.icon_news);
			if(msgs_data!=null)
				viewholder.tv_content.setText(main.getString(R.string.order_cancel_msg));
			break;
		case GMsg.OFFICAIL_ONLINE_GAIPAI:
		case GMsg.ONLINE_KF_GAIPAIL:
			viewholder.tv_name.setText(main.getString(R.string.msg_remind));
			viewholder.head.setImageResource(R.drawable.icon_news);
			if(msgs_data!=null)
				viewholder.tv_content.setText(String.format(main.getString(R.string.order_gaipai_msg), msgs_data.getReason()));
			break;
		case GMsg.OFFICAIL_ONLINE_ONCAR:
			viewholder.tv_name.setText(main.getString(R.string.msg_remind));
			viewholder.head.setImageResource(R.drawable.icon_news);
			if(msgs_data!=null)
				viewholder.tv_content.setText(main.getString(R.string.msg_oncar));
			break;
		case GMsg.OFFICAIL_ONLINE_OFFCAR:
			viewholder.tv_name.setText(main.getString(R.string.msg_remind));
			viewholder.head.setImageResource(R.drawable.icon_news);
			if(msgs_data!=null)
				viewholder.tv_content.setText(main.getString(R.string.msg_offcar));
			break;
		case GMsg.ONLINE_FINISH:
		case GMsg.OFFICAIL_ONLINE_FINISH:
			viewholder.tv_name.setText(main.getString(R.string.msg_remind));
			viewholder.head.setImageResource(R.drawable.icon_news);
			if(msgs_data!=null)
				viewholder.tv_content.setText(main.getString(R.string.order_finish));
			break;
		case GMsg.ONLINE_XIADAN:
		case GMsg.ONLINE_BG_ORDER:
			viewholder.tv_name.setText(main.getString(R.string.msg_remind));
			viewholder.head.setImageResource(R.drawable.icon_news);
			viewholder.tv_content.setText(main.getString(R.string.msg_xiadan));
			break;
		case GMsg.ONLINE_BUSY:
			viewholder.tv_name.setText(main.getString(R.string.msg_remind));
			viewholder.head.setImageResource(R.drawable.icon_news);
			if(msgs_data.getText()!=null)
			viewholder.tv_content.setText(msgs_data.getText());
			break;
		case GMsg.ONLINE_KF_CHANGE:
			viewholder.tv_name.setText(main.getString(R.string.msg_remind));
			viewholder.head.setImageResource(R.drawable.icon_news);
			viewholder.tv_content.setText(main.getString(R.string.msg_change));
			break;
//		case GMsg.ONLINE_OUTLINE_PAY_SUCC:
//			viewholder.tv_name.setText(main.getString(R.string.msg_remind));
//			viewholder.head.setImageResource(R.drawable.icon_news);
//			viewholder.tv_content.setText(main.getString(R.string.order_line_msg));
//			break;
//		case GMsg.ONLINE_ONTLINE_PAY_SUCC:
//			viewholder.tv_name.setText(main.getString(R.string.msg_remind));
//			viewholder.head.setImageResource(R.drawable.icon_news);
//			viewholder.tv_content.setText(main.getString(R.string.order_online_msg));
//			break;
		case GMsg.ONLINE_NEW_UNPROCESSED:
			viewholder.tv_name.setText(main.getString(R.string.msg_remind));
			viewholder.head.setImageResource(R.drawable.icon_news);
			viewholder.tv_content.setText(main.getString(R.string.msg_new_unprocessed));
			break;
		case GMsg.ONLINE_UNPROCESSED_RESULT:
			viewholder.tv_name.setText(main.getString(R.string.msg_remind));
			viewholder.head.setImageResource(R.drawable.icon_news);
			String result="";
			if (msgs_data != null) {
				 result=msgs_data.getResult();
			}
			if(result.equals(Value.MSG_RESULT_PASS)){
				viewholder.tv_content.setText(main.getString(R.string.msg_unprocessed_pass));
			
			}else if(result.equals(Value.MSG_RESULT_NOPASS)){
				viewholder.tv_content.setText(main.getString(R.string.msg_unprocessed_nopass));
			}	
			
			break;
		case GMsg.OFFICAIL_ONLINE_ROUND_TRIP:
			viewholder.tv_name.setText(main.getString(R.string.msg_remind));
			viewholder.head.setImageResource(R.drawable.icon_news);
			viewholder.tv_content.setText(String.format(main.getString(R.string.msg_round_trip), msgs_data.getRound_trip().equals(Value.ROUND_TRIP)?
					main.getString(R.string.text_round_trip):main.getString(R.string.text_round_trip_no)));
			break;
		case GMsg.OFFICAIL_AA:
			viewholder.tv_name.setText(main.getString(R.string.msg_system_remind));
			viewholder.head.setImageResource(R.drawable.icon_massg_m);
			if(msgs_data!=null){
				viewholder.tv_content.setText(Html.fromHtml(msgs_data.getContent()));
				viewholder.tv_content.setSingleLine(false);
				viewholder.iv_jump.setVisibility(View.GONE);
			}
			break;
		}
		viewholder.tv_time.setText(DateFormat.getTimeDifference(list.get(position).get(DBcolumns.MESSAGE_TIME)));
		return convertView;

	}

	public class ViewHolder {
		TextView tv_name,tv_content,tv_time;
		CircleImageView head;
		ImageView iv_jump;
	}

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
//				main.startActivity(new Intent(main, AddAddressActivity.class));
				break;
			}
		};
	};

	/**
	 * 计算时间，显示今天，昨天，具体日期
	 */
	public String getTime(String time) {
		//文本时间
		Date date=new Date();
		date.setTime(Long.parseLong(time));
		//当前时间
		Date currentdate=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			date = sdf.parse(time);
		} catch (ParseException e) {
		}
		//时间差
		int differenceDay = currentdate.getDay()-date.getDay();
		switch(differenceDay){
		case 0:
			return "今天 "+date.getHours()+":"+date.getMinutes();
		case 1:
			return "昨天 "+date.getHours()+":"+date.getMinutes();
		default:
			return sdf.format(date);
		}
	}
}
